/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.agentslab.appstarter;

import aa.core.Actor;
import aa.core.ActorMessage;
import aa.core.ActorName;
import aa.core.Contract;
import aa.core.CyberOrg;
import aa.tool.CyberOrgTuple;
import com.agentslab.constants.APIConstants;
import games.mapacman.client.MaPacman;
import games.mapacman.client.MaPacmanSimulator;
import games.mapacman.server.DummyAgent;
import games.mapacman.server.MaPacmancreateaccount;
import games.mapacman.server.ZoneDivider;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JList;
import javax.swing.JOptionPane;
import marauroa.client.BannedAddressException;
import marauroa.client.TimeoutException;
import marauroa.common.Configuration;
import marauroa.common.net.InvalidVersionException;

/**
 *
 * @author mehadihasan
 */
public class GUIActor extends Actor {

    private MainFrame mainFrame;
    private static MaPacman client;
    private int v=0;
    private String s1URL;
    private String s2URL;

    public GUIActor() {
        mainFrame = new MainFrame();
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                mainFrame.setVisible(true);
            }
        });
        Configuration conf;
        try {
            conf = Configuration.getConfiguration();
            s1URL = conf.get("server1_url");
            s2URL = conf.get("server2_url");
            APIConstants.serverHost = s1URL;
            APIConstants.serverHost1 = s2URL;
        } catch (IOException ex) {
            Logger.getLogger(GUIActor.class.getName()).log(Level.SEVERE, null, ex);
        }
                    
    }

    public void updateGameList(ArrayList<String> gameList, String startTime) {
        mainFrame.getGameListPanel().getGameList().setListData(gameList.toArray());
        System.out.println((System.currentTimeMillis() - Long.parseLong(startTime)));
    }

    public void updateServerList(String gameName, String startTime) {
        Object[] servers = null;

        mainFrame.getServerListPanel().getGameNameLabel().setText(gameName);
        try {
            servers = (Object[]) call(APIConstants.gameBroker, "getServerList", gameName, new Contract(1000000, 1000, 1));
        } catch (Exception e) {
            showStatus("Exception occured while loading server list for " + gameName);
        }
        if (servers == null) {
            showStatus("Server List for " + gameName + " is null");
        }

        mainFrame.getServerListPanel().getServerList().setListData(servers);
        mainFrame.getPartyTab().setComponentAt(1, mainFrame.getServerListPanel());

        mainFrame.getServerListPanel().updateUI();
        System.out.println((System.currentTimeMillis() - Long.parseLong(startTime)));


    }

    public void updateResourceList(CyberOrgTuple[] cyberOrgTuples, String startTime) {
        mainFrame.getResourceListPanel().getResourceList().setListData(cyberOrgTuples);
        System.out.println((System.currentTimeMillis() - Long.parseLong(startTime)));
    }

    public void dynamicAllocation(CyberOrgTuple[] cyberOrgTuples, String startTime) {
        mainFrame.getResourceListPanel().getResourceList().setListData(cyberOrgTuples);
        System.out.println((System.currentTimeMillis() - Long.parseLong(startTime)));

        long temp = 9999999;
        CyberOrgTuple tempTuple = null;
        for (CyberOrgTuple cyberOrgTuple : cyberOrgTuples) {
            if (temp > cyberOrgTuple.getContract().getPrice()) {
                temp = cyberOrgTuple.getContract().getPrice();
                tempTuple = cyberOrgTuple;
            }
        }
        Object[] objaAgs = {};

        ActorName[] actorNames = new ActorName[1];
        actorNames[0] = new ActorName(APIConstants.platform.createActor(APIConstants.platformCyb.getFacilitator(), "games.mapacman.server.Server", objaAgs));

        CyberOrg cyb = APIConstants.platformCyb.isolate(1000, actorNames, new Contract(new Long(1000000), new Long(1000), new Long(1000)));

        cyb.migrate(new ActorName(tempTuple.getDestination(), 2), new Contract(new Long(1000000), new Long(1000), new Long(1000)));

        try {
            Thread.sleep(5000);
        } catch (InterruptedException ex) {
            Logger.getLogger(GUIActor.class.getName()).log(Level.SEVERE, null, ex);
        }
        Object[] argsreg = new Object[5];
        argsreg[0] = tempTuple;
        argsreg[1] = new Contract(new Long(9000000), new Long(9000), new Long(100));
        argsreg[2] = "startServer";
        argsreg[3] = actorNames[0];
        argsreg[4] = "" + System.currentTimeMillis();
        ActorMessage am_reg = new ActorMessage(APIConstants.platform.getActorNameOfPlatform(),
                APIConstants.resourceBroker,
                "negotiate",
                argsreg,
                false);

        APIConstants.platform.sendMessage(am_reg);
    }

    public void showStatus(String status) {

        JOptionPane.showMessageDialog(null, status, "Status", JOptionPane.INFORMATION_MESSAGE);
    }

    public void showStatus(String status, String startTime) {

        System.out.println((System.currentTimeMillis() - Long.parseLong(startTime)));
        JOptionPane.showMessageDialog(null, status, "Status", JOptionPane.INFORMATION_MESSAGE);
    }

    public void startServer(CyberOrgTuple cyberOrgTuple, String startTime) {
        send(APIConstants.serverActor, "startServer");
        showStatus("Negotiation successful!! Game server is up and running...");
        System.out.println((System.currentTimeMillis() - Long.parseLong(startTime)));
    }

    public void needResource(String nada) {
        Object[] argsreg = new Object[5];
        argsreg[0] = new Contract(new Long(9000000), new Long(9000), new Long(100));
        argsreg[1] = "pacman";
        argsreg[2] = "dynamicAllocation";
        argsreg[3] = APIConstants.guiActor;
        argsreg[4] = "" + System.currentTimeMillis();
        ActorMessage am_reg = new ActorMessage(APIConstants.platform.getActorNameOfPlatform(),
                APIConstants.resourceBroker,
                "getNewResource",
                argsreg,
                false);

        APIConstants.platform.sendMessage(am_reg);
        
    }

    public void quickStartServer(String nada) {

        ZoneDivider divider = ZoneDivider.get();
        divider.createZoneBox("start", s2URL, 0, 0, 30, 32);
        divider.createZoneBox("start", s1URL, 0, 32, 70, 60);
        List<String> list = new ArrayList<String>();
        list.add(s2URL);
        divider.setAvailableIPs(list);
        //divider.createInitialZoneBoxes("start", 0, 32, 70, 60);

        send(APIConstants.serverActor, "startServer");
    }

    public void startClient(String user, String pass, String startTime) {
        //APIConstants.user = user;
        //APIConstants.pass = pass;
        user = "test";
        pass = "test";
        System.out.println((System.currentTimeMillis() - Long.parseLong(startTime)));
        //client = new MaPacman(false, "test1", "test1", APIConstants.serverHost, APIConstants.serverPort);

        for (int i = 355+v; i <= 450+v; i++) {
            //    if(i!=4)
            new MaPacmanSimulator(false, user + i, pass + i, s1URL, APIConstants.serverPort);
            try {
                Thread.sleep(1000);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        v = 50;
    }
}
